package alchemist.dao.local;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Repository;

import alchemist.dao.CombinationsDao;
import alchemist.model.Combination;
import alchemist.model.Element;

@Repository
public class JsonDao implements CombinationsDao
{

    private DataCache cache;
    private static final Logger LOG = Logger.getLogger( JsonDao.class.getName() );

    @Override
    public Set< Element > getResults( Combination combination )
    {
        return cache.get( combination );
    }
    
    @Override
    public Set< Element > getBasicElements() 
    {
        return cache.getBasicElements();
    };
    
    @Override
    public Set< Element > getElements( Set< String > elementIds )
    {
        final Set< Element > result = new HashSet< Element >();
        for ( String elementId : elementIds )
        {
            result.add( cache.get( elementId ) );
        }
        return result;
    }
    
    @Override
    public Element retrieveElement( String id )
    {
        return cache.get( id );
    }
    
    @Override
    public Integer getTotalElements()
    {
    	return cache.getElements().size();
    }

    @PostConstruct
    public void init()
    {
        final ObjectMapper om = new ObjectMapper();
        try
        {
            JsonDataHolder dh = om.readValue( 
                    this.getClass().getResource( "/alchemist/combinations.json" ), JsonDataHolder.class );
            this.cache = transform( dh );
        }
        catch ( JsonParseException e )
        {
            LOG.severe( e.getMessage() );
        }
        catch ( JsonMappingException e )
        {
            LOG.severe( e.getMessage() );
        }
        catch ( IOException e )
        {
            LOG.severe( e.getMessage() );
        }
    }

    private DataCache transform( JsonDataHolder dh )
    {
        final DataCache cache = new DataCache();
        if ( dh != null )
        {
            if ( dh.getElements() != null )
            {
                for ( int i = 0; i < dh.getElements().length; i++ )
                {
                    cache.put( dh.getElements()[ i ] );
                }
            }
            if ( dh.getCombinations() != null )
            {
                for ( int i = 0; i < dh.getCombinations().length; i++ )
                {
                    cache.put(
                            dh.getCombinations()[ i ],
                            transform( dh.getCombinations()[ i ].getResult(),
                                    cache.getElements() ) );
                }
            }
            if ( dh.getBasicElements() != null )
            {
                for ( int i = 0; i < dh.getBasicElements().length; i++ )
                {
                    cache.addBasic( cache.getElements().get( dh.getBasicElements()[ i ] ) );
                }
            }
        }
        return cache;
    }

    private Set< Element > transform( Set< String > elements,
            Map< String, Element > elementsMap )
    {
        final Set< Element > result = new HashSet< Element >();
        for ( String id : elements )
        {
            result.add( elementsMap.get( id ) );
        }
        return result;
    }

}
